package com.hudson.plugin.server.hibernateDB.internal.domain.dao;

import com.hudson.plugin.server.hibernateDB.domain.dao.ProjectsDAOInterface;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.ProjectsEntity;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * Projects entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see .Projects
 */

public class ProjectsDAO extends HibernateDaoSupport implements ProjectsDAOInterface {
    private static final Logger log = LoggerFactory
            .getLogger(ProjectsDAO.class);

    protected void initDao() {
        // do nothing
    }

    public void save(ProjectsEntity transientInstance) {
        log.debug("saving Projects instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(ProjectsEntity persistentInstance) {
        log.debug("deleting Projects instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public ProjectsEntity findById(Integer id) {
        log.debug("getting Projects instance with id: " + id);
        try {
            ProjectsEntity instance = (ProjectsEntity) getHibernateTemplate().load(
                    ProjectsEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(ProjectsEntity instance) {
        log.debug("finding Projects instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Projects instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from ProjectsEntity as model where model."
                    + propertyName + " = ?";
            List results = getHibernateTemplate().find(queryString, value);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByProjectName(Object projectName) {
        return findByProperty(PROJECT_NAME, projectName);
    }

    public List findByChildProjects(Object childProjects) {
        return findByProperty(CHILD_PROJECTS, childProjects);
    }

    public List findByParentProjects(Object parentProjects) {
        return findByProperty(PARENT_PROJECTS, parentProjects);
    }

    public List findByType(Object type) {
        return findByProperty(TYPE, type);
    }

    public List findByUrl(Object url) {
        return findByProperty(URL, url);
    }

    public List findAll() {
        log.debug("finding all Projects instances");
        try {
            String queryString = "from ProjectsEntity";
            List results = getHibernateTemplate().find(queryString);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public ProjectsEntity merge(ProjectsEntity detachedInstance) {
        log.debug("merging Projects instance");
        try {
            ProjectsEntity result = (ProjectsEntity) getHibernateTemplate().merge(
                    detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(ProjectsEntity instance) {
        log.debug("attaching dirty Projects instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(ProjectsEntity instance) {
        log.debug("attaching clean Projects instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static ProjectsDAOInterface getFromApplicationContext(ApplicationContext ctx) {
        return (ProjectsDAOInterface) ctx.getBean("ProjectsDAO");
    }

    public List<Integer> findProjectsEntityByHudsonIdAndName(Integer hudsonId, String projectName) {
        log.debug("finding ProjectsEntity By HudsonId And Name");
        try {
            Query query = this.getSession().createQuery(
                    "select p.id from ProjectsEntity p where p.projectName = :project_name and p.hudsonsByHudsonId.id = :hudson_id");
            query.setParameter("project_name", projectName.trim());
            query.setParameter("hudson_id", hudsonId);
            List<Integer> projectsId = query.list();
            if (projectsId == null) {
                return new ArrayList();
            }

            return projectsId;
        } catch (RuntimeException re) {
            log.error("finding ProjectsEntity By HudsonId And Name failed", re);
            throw re;
        }
    }
}